package com.qf.controller;

import com.qf.annotations.RequestMapping;
import com.qf.pojo.*;
import com.qf.service.AddressService;
import com.qf.service.CartService;
import com.qf.service.Impl.AddressServiceImpl;
import com.qf.service.Impl.CartServiceImpl;
import com.qf.service.Impl.OderServiceImpl;
import com.qf.service.OderService;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/order")
public class OrderController {
    @Resource
    CartService cartService ;
    @Resource
    AddressService addressService ;
    @Resource
    OderService oderService ;

    @RequestMapping("/preView")
    public String preView(HttpServletRequest request, HttpServletResponse response) {
        TbUser loginUser = (TbUser) request.getSession().getAttribute("loginUser");
        long uid = loginUser.getId();
        if (request.getSession().getAttribute("cart") == null) {
            List<CartItem> cartItemList = cartService.findCartByUid(uid);
            request.getSession().setAttribute("cart", cartItemList);
        }
        if (request.getSession().getAttribute("addressList") == null) {
            List<TbAddress> addressList = addressService.findAllAddresByUid(uid);
            request.getSession().setAttribute("addressList", addressList);
        }
        return "forward:/order.jsp";
    }

    @RequestMapping("/create")
    public String createOrder(HttpServletRequest request, HttpServletResponse response) {
        List<CartItem> cart = (List<CartItem>) request.getSession().getAttribute("cart");
        String aid = request.getParameter("aid");
        Order order = oderService.createOrder(cart, aid);
        request.getSession().setAttribute("order", order);
        return "redirect:/orderSuccess.jsp";
    }

    @RequestMapping("/show")
    public String showOrder(HttpServletRequest request, HttpServletResponse response) {
        TbUser loginUser = (TbUser) request.getSession().getAttribute("loginUser");
        long id = loginUser.getId();
        List<TbOrder> tbOrders = oderService.showOrder(id);


        request.getSession().setAttribute("ordersList", tbOrders);
        return "forward:/orderList.jsp";
    }

    @RequestMapping("/chaxdingdan")
    public String showdingdan(HttpServletRequest request, HttpServletResponse response) {
        String pid = (request.getParameter("id"));
        TbOrder showkz = oderService.showkz(pid);
        List<TbOrderdetail> showde = oderService.showde(pid);
        request.getSession().setAttribute("order", showkz);
        request.getSession().setAttribute("orderdetial", showde);
        return "forward:/orderDetail.jsp";
    }
    @RequestMapping("/deletegouwu")
    public String deletegouwu(HttpServletRequest request, HttpServletResponse response){

        String id = request.getParameter("id");
        oderService.deletegouwu(id);

        return "redirect:/order/show";

    }
}
